import { areaList } from "@vant/area-data";
import dayjs from "dayjs";
import Toast from "@vant/weapp/toast/toast";
import { request } from "../../utils/request";
Page({
  data: {
    areaList,
    showPosition: false,
    showPosition2: false,
    showTime: false,
    minHour: 8,
    maxHour: 22,
    minDate: new Date().getTime(),
    maxDate: new Date(2023, 12, 30).getTime(),
    currentDate: new Date().getTime(),
    formatter(type: any, value: any) {
      if (type === "year") {
        return `${value}年`;
      }
      if (type === "month") {
        return `${value}月`;
      }
      if (type === "day") {
        return `${value}日`;
      }
      if (type === "hour") {
        return `${value}时`;
      }
      if (type === "minute") {
        return `${value}分`;
      }
      return value;
    },
    showPay: false,
    list: [1, 2, 3, 4, 5, 6, 7, 8, 9],
    password: <Array<number>>[],
    info: {
      user: "",
      type: 1,
      position: "",
      position2: "",
      time: "",
      userName: "",
      number: "",
      price: 69,
    },
  },
  selectPosition() {
    this.setData({
      showPosition: true,
    });
  },
  positionClose() {
    this.setData({
      showPosition: false,
    });
  },
  positionConfirm(e: any) {
    let position = "";
    e.detail.values.forEach((item: any) => {
      position = position + " " + item.name;
    });
    this.setData({
      "info.position": position,
      showPosition: false,
    });
  },
  positionCancel() {
    this.setData({
      showPosition: false,
    });
  },

  selectPosition2() {
    this.setData({
      showPosition2: true,
    });
  },
  positionClose2() {
    this.setData({
      showPosition2: false,
    });
  },
  positionConfirm2(e: any) {
    let position2 = "";
    e.detail.values.forEach((item: any) => {
      position2 = position2 + " " + item.name;
    });
    this.setData({
      "info.position2": position2,
      showPosition2: false,
    });
  },
  positionCancel2() {
    this.setData({
      showPosition2: false,
    });
  },
  onInput(event: any) {
    this.setData({
      currentDate: event.detail,
    });
  },
  selectTime() {
    this.setData({
      showTime: true,
    });
  },
  timeClose() {
    this.setData({
      showTime: false,
    });
  },
  timeConfrim(e: any) {
    let time = dayjs(e.detail).format("YYYY-MM-DD HH:MM");
    this.setData({
      "info.time": time,
      showTime: false,
    });
  },
  infoChange(e: any) {
    let name = e.currentTarget.dataset.name;
    if (name == "userName") {
      this.setData({
        "info.userName": e.detail,
      });
    }
    if (name == "number") {
      this.setData({
        "info.number": e.detail,
      });
    }
  },
  pay() {
    Toast.loading({
      message: "支付中...",
      duration: 1000,
      forbidClick: true,
      loadingType: "spinner",
      onClose: () => {
        this.setData({
          showPay: true,
        });
      },
    });
  },
  closePopup() {
    this.setData({ showPay: false });
  },
  async getPassword(e: any) {
    if (this.data.password.length == 5) {
      this.data.password.push(e.currentTarget.dataset.number);
      this.setData({ password: this.data.password, showPay: false });
      try {
        let info = this.data.info;
        let { id } = wx.getStorageSync("userInfo");
        info.user = id;
        await request("/order", "POST", {
          ...info,
        });
        Toast.success({
          type: "success",
          message: "支付成功",
          duration: 1000,
          onClose: async () => {
            this.setData({ password: [] });
            wx.reLaunch({ url: "/pages/order/order" });
          },
        });
      } catch (error) {
        Toast.success({
          type: "fail",
          message: "支付失败",
          duration: 1000,
          onClose: async () => {
            this.setData({ password: [] });
          },
        });
      }
    } else if (this.data.password.length < 5) {
      this.data.password.push(e.currentTarget.dataset.number);
      this.setData({ password: this.data.password });
    } else {
      return;
    }
  },
  removePassword() {
    this.data.password.pop();
    this.setData({ password: this.data.password });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {},
});
